#include <leveldb/db.h>

#include <iostream>

int main()
{
	leveldb::DB *db;

	leveldb::Options options;
	options.create_if_missing = true;

	leveldb::Status status =
		leveldb::DB::Open(options, "test.leveldb", &db);
	if (!status.ok()) {
		std::cerr << "Open failed: " << status.ToString() << std::endl;
		return 1;
	}

	std::string key = "key1";
	std::string value = "value1";
	status = db->Put(leveldb::WriteOptions(), key, value);
	if (!status.ok()) {
		std::cerr << "Write failed: " << status.ToString() << std::endl;
		return 1;
	}

	std::string value_read;
	status = db->Get(leveldb::ReadOptions(), key, &value_read);
	if (status.ok()) {
		std::cout << "Read success: " << key << " => " << value_read
			  << std::endl;
	} else if (status.IsNotFound()) {
		std::cerr << "Key not found: " << key << std::endl;
	} else {
		std::cerr << "Read failed: " << status.ToString() << std::endl;
		return 1;
	}

	// 删除键值对
	status = db->Delete(leveldb::WriteOptions(), key);
	if (!status.ok()) {
		std::cerr << "Delete failed: " << status.ToString()
			  << std::endl;
		return 1;
	}

	// 关闭数据库
	delete db;

	return 0;
}
